using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using smartBrowser.Ontology;
using smartBrowser.Util;

namespace smartBrowser.GUI
{
    public partial class ManagerSmartOntology : Form
    {        

        public ManagerSmartOntology(Ontology.Ontology ontology)
        {
            InitializeComponent();            
            this.loadOntology(ontology);
        }

        private void loadOntology(Ontology.Ontology ontologia)
        {                        
            TreeNode node = null;

            foreach (String suj in ontologia.Ontologia.Keys)
            {                
                node = new TreeNode(suj);
                node.Name = suj;
                this.treeView1.Nodes.Add(node);
                
                Tag  concept = (Tag)ontologia.Ontologia[suj];
                
                TreeNode nodeRelation = new TreeNode("Relations");
                TreeNode nodeProperty = new TreeNode("Properties");
                node.Nodes.Add(nodeRelation);
                node.Nodes.Add(nodeProperty);

                foreach(State st in concept.RelationList)
                {                    

                    TreeNode node1 = new TreeNode(st.Object.Concept);                    
                    nodeRelation.Nodes.Add(node1);
                }


                foreach (State st in concept.Properties)
                {

                    TreeNode node1 = new TreeNode(st.Predicate.Concept);
                    node1.Nodes.Add(new TreeNode(st.Object.Concept));
                    nodeProperty.Nodes.Add(node1);
                }

            }

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                this.contextMenuStrip1.Items[0].Enabled = true;
                this.contextMenuStrip1.Items[2].Enabled = true;
            }
            else
            {
                this.contextMenuStrip1.Items[0].Enabled = false;
                this.contextMenuStrip1.Items[2].Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gettingsMoreInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                MessageBox.Show("Gettinhs more info about the \"" + this.treeView1.SelectedNode.Text + "\" concept in swoogle ontologies ...");
            }            
        }

        private void removeConceptTSMI_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void bSearch_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            String concept = this.tbConcept.Text;
            if (smartBrowser.GUI.SmartBrowser.Instance.Ontologia.Ontologia.ContainsKey(concept.ToLower()))
            {                
                Tag tag = (Tag)smartBrowser.GUI.SmartBrowser.Instance.Ontologia.Ontologia[concept.ToLower()];                
                this.listBox1.Items.Add(concept.ToLower());                
            }
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            String concept = (String)this.listBox1.SelectedItem;
            TreeNode[] nodes = this.treeView1.Nodes.Find(concept.ToLower(), true);  
            foreach (TreeNode node in nodes)
            {                
                node.Expand();
            }
        }
    }
}